import pygame
import random
SCREEN_RECT = pygame.Rect(0,0,480,700)#背景常量
ENEMY_EVENT = pygame.USEREVENT#小敌机事件
ENEMY_EVENT1 = pygame.USEREVENT+2#大敌机事件
FIRE_BULLET_EVENT = pygame.USEREVENT + 1#子弹事件




# 背景
class Background(pygame.sprite.Sprite):
	def __init__(self,image='./img/background.png',is_two=False):
		super().__init__()

		self.image = pygame.image.load(image)
		self.rect = self.image.get_rect()
		# 判断是否为第二图
		if is_two == True:
			self.rect.y = -SCREEN_RECT.height
	# 滚动
	def update(self):
		self.rect.y += 2

		if self.rect.y >= SCREEN_RECT.height:
			self.rect.y = -SCREEN_RECT.height
# 敌方飞机
class Enemy(pygame.sprite.Sprite):
	
	def __init__(self,image):
		super().__init__()
		self.image = image
		self.rect = self.image.get_rect()
		# 飞机速度
		self.speed = random.randint(1,4)
		# 飞机出现位置
		self.rect.x = random.randint(0,(SCREEN_RECT.width-self.rect.width))
		
	# 飞机下落
	def update(self):
		
		self.rect.y +=self.speed
		# 销毁飞机
		if self.rect.y >= SCREEN_RECT.height:
			self.kill()
# 大敌机
class BigEnemy(pygame.sprite.Sprite):
	
	def __init__(self,image):
		super().__init__()
		self.image = image
		self.rect = self.image.get_rect()
		# 飞机速度
		self.speed = random.randint(1,2)
		# 飞机出现位置
		self.rect.x = random.randint(0,(SCREEN_RECT.width-self.rect.width))
		
	# 飞机下落
	def update(self):
		
		self.rect.y +=self.speed
		
		# 销毁飞机
		if self.rect.y >= SCREEN_RECT.height:
			self.kill()
#我方飞机
class Hero(pygame.sprite.Sprite):
	def __init__(self,image):
		super().__init__()

		self.image = image
		self.rect = self.image.get_rect()
		# 飞机初始位置
		self.rect.x = SCREEN_RECT.width/2 - self.rect.width/2
		self.rect.y = SCREEN_RECT.height - self.rect.height - 120

		#  子弹
		
		self.bullet_group = pygame.sprite.Group()

	def update(self):
		# 移动我方飞机
		self.rect.x += self.speedx
		self.rect.y += self.speedy

		if self.rect.x < 0:
			self.rect.x = 0
		elif self.rect.x > SCREEN_RECT.width - self.rect.width:
			self.rect.x = SCREEN_RECT.width - self.rect.width

		if self.rect.y < 0:
			self.rect.y = 0
		elif self.rect.y > SCREEN_RECT.height - self.rect.height:
			self.rect.y = SCREEN_RECT.height - self.rect.height


	def fire(self,bullet_image):
		self.bullet = Bullet(bullet_image)
		# 子弹初始位置
		self.bullet.rect.centerx = self.rect.centerx
		self.bullet.rect.y = self.rect.y

		# 添加子弹精灵组
		self.bullet_group.add(self.bullet)
# 子弹
class Bullet(pygame.sprite.Sprite):
	def __init__(self,image,speed=5):
		super().__init__()
		self.image = image
		self.rect = self.image.get_rect()
		self.speed = speed
		# 子弹初始位置
	def update(self):
		self.rect.y -= self.speed
		# self.rect.x -= self.speed

		if self.rect.y < -self.rect.height:
			self.kill()

class Gameover(pygame.sprite.Sprite):
	def __init__(self,image='./img/Gameover.png'):
		super().__init__()
		self.image = pygame.image.load(image)
		self.rect = self.image.get_rect()

# class Startback(pygame.sprite.Sprite):
# 	def __init__(self,image='./img/Gameover.png'):
# 		super().__init__()
# 		self.image = pygame.image.load(image)
# 		self.rect = self.image.get_rect()
